 package com.shopyourstyle.activities;

//[TODO]
// 1. In Edit/ Not In Edit need to be able to switch
// 2. Next Page/ Previous Page
// 3. Able to dump the left-corner position after leaving edit mode
// 4. Multi-lab view switching.

import android.app.Activity;

import java.util.Random;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ToggleButton;

import com.shopyourstyle.R;

public class SingleThemePage extends Activity {

    /** Called when the activity is first created. */
	private MagView[] magViewArr = new MagView[3];
	private SurfaceView mySurfaceView;
	private Button tag1Btn,tag2Btn,tag3Btn;
	private ToggleButton editBtn;
	private int currentViewNum;
	private int posX[][] ={  
			{ 58 , 265 , 4 , 285 , 255 , 0},
			{ 65 , 279 , 19 , 280 , 265 , 34},
			{ 279, 44 , 86 , 35 , 2 , 0 }			
	};
	private int posY[][] = {
			{ 463 , 221 , 28 , 451 , 9 , 0 },
			{ 319 , 472 , 502 , 11 , 263 , -4 },
			{ 0 , 374 , 183 , -33 , 477 , 0 }			
	};

	
	private boolean checkResource( String _resName) {
		
		Drawable _pic;
		int resID = this.getResources().getIdentifier( _resName , "drawable", this.getPackageName() );
		Log.e("@checkResource" , "_resName:" + _resName + ", rval:" + resID);
		if (resID==0) 	return false;
		else			return true;

		//_pic = this.getResources().getDrawable( resID );
	}
	public MagView createMagView(int number) {
		
		MagView myMagView = new MagView(this , mySurfaceView);
		//[To Allen/To Jimmy] All Items added here.
		String tmpStr;
		Random generator = new Random();
		
		this.checkResource("pig");
		
		String pg = Integer.toString(number);
		tmpStr = "p" + pg + "_bg";
		if (this.checkResource(tmpStr))
				myMagView.setBackground ( tmpStr );

		tmpStr = "p" + pg + "_bar";
		if (this.checkResource(tmpStr))
				myMagView.setBar( tmpStr );
		
		for (int i=1;i<=6;++i) {
			tmpStr = "p" + pg + "_" + Integer.toString(i);
			if (this.checkResource(tmpStr)) {
		    	MagItem item0 = new MagItem( this , tmpStr, generator.nextInt(6) , generator.nextInt(6) , generator.nextInt(6) );
		    	//item0.setParameters(54,0 , 255, 140);
		    	item0.setParameters( posX[number-1][i-1] , posY[number-1][i-1] , 255, -1 );
		    	item0.setAllenString( new String[]{ 
		    			"p" + pg + "_" + i + " pic" ,
		    			"p" + pg + "_" + i + " fit list" ,
		    			"p" + pg + "_" + i + " comment list" ,
		    			} );

		    	myMagView.addMagItem( item0 );
			}
		}
		
    	return myMagView;
	}
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	setContentView(R.layout.singlethemepage);
    	mySurfaceView = (SurfaceView)findViewById(R.id.surfaceView1 );
		mySurfaceView.setFocusable(true); // make sure we get key events
		mySurfaceView.setFocusableInTouchMode(true); // make sure we get touch events
    	
    	magViewArr[ 0 ] = this.createMagView( 1 );
    	magViewArr[ 1 ] = this.createMagView( 2 );
    	magViewArr[ 2 ] = this.createMagView( 3 );
    	
    	findView();
    	changeMagView ( 2 , true);
    }
    
    private void changeMagView(int num, boolean editable) {
    	for (int i=0;i<3;++i) {
    		if (magViewArr[i]==null) continue;
        	magViewArr[i].setEditable( false );
        	magViewArr[i].setInActive( false );
    	}
    	currentViewNum = num;
    	magViewArr[num].setInActive( true );
    	magViewArr[num].setEditable( editable );
    	magViewArr[num].setSurfaceView( mySurfaceView );
    	//Call redraw immediately
    	magViewArr[num].redraw();
   		editBtn.setChecked(editable);
    }
    
    private void findView() {
    	tag1Btn = (Button)findViewById(R.id.buttonTag1 );
    	tag2Btn = (Button)findViewById(R.id.buttonTag2 );
    	tag3Btn = (Button)findViewById(R.id.buttonTag3 );
    	editBtn = (ToggleButton)findViewById(R.id.toggleButtonEditing );
    	
    	tag1Btn.setOnClickListener(tag1BtnLs);
    	tag2Btn.setOnClickListener(tag2BtnLs);
    	tag3Btn.setOnClickListener(tag3BtnLs);
    	editBtn.setOnClickListener(editBtnLs);
    }
    
    
	private Button.OnClickListener tag1BtnLs=new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			//Log.e ("@onClick" , " Clicking 0");
			changeMagView ( 0 , false );
		}
	};
	private Button.OnClickListener tag2BtnLs=new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			changeMagView ( 1 , false );
		}
	};
	private Button.OnClickListener tag3BtnLs=new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			changeMagView ( 2 , false );
		}
	};
	
	private ToggleButton.OnClickListener  editBtnLs= new ToggleButton.OnClickListener() {
		@Override
		public void onClick(View v)
		{
			//Dumping current display information
			for (int i=0;i<6;++i) {
				if (magViewArr[currentViewNum].items[i]==null) continue;
				Log.e ( "@editBtnLs" ,
						"item " + i + " - (" + magViewArr[currentViewNum].items[i].getX() + "," + magViewArr[currentViewNum].items[i].getY() + ")"  );
			}
			
			if (editBtn.getText().equals(editBtn.getTextOn()) ){
				changeMagView ( currentViewNum , true );
			}
			else {
				changeMagView ( currentViewNum , false );
			}
				//Toast.makeText(getBaseContext(),
				//"Button is "+tb.getText().toString(),
				//Toast.LENGTH_LONG).show();
		}
	};
	                                
}